package xlj.lib.android.base.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import xlj.lib.android.base.R;


/**
 * tab选项卡
 * Created by deve on 16/9/5.
 */
public class TabItem extends FrameLayout {
    private TextView redotV;
    private ImageView img;
    private TextView txt;

    private Drawable imgD;
    private String txtS;

    public TabItem(Context context) {
        super(context);
        init(context);
    }

    public TabItem(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TabItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabItem, defStyleAttr, 0);

        imgD = a.getDrawable(R.styleable.TabItem_img);
        txtS = a.getString(R.styleable.TabItem_txt);

        a.recycle();
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.view_tab,this);
        redotV = (TextView) findViewById(R.id.tab_redot);
        img = (ImageView) findViewById(R.id.tab_img);
        txt = (TextView) findViewById(R.id.tab_txt);
        if (imgD != null) {
            img.setImageDrawable(imgD);
        }
        if (!TextUtils.isEmpty(txtS)) {
            txt.setText(txtS);
        }
        showRedot(false);
    }

    public void  setTextColor(int textColorRes) {
        txt.setTextColor(getResources().getColorStateList(textColorRes));
    }

    public void setImg(int drawableResource) {
        img.setImageResource(drawableResource);
    }

    public void setTxt(String txtStr) {
        txt.setText(txtStr);
    }

    public void showRedot(boolean isShow) {
        redotV.setVisibility(isShow ? VISIBLE : GONE);
    }
}
